
package com.sm.shoplook.controller.aboutus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.domain.about.PartnerCase;
import com.sm.shoplook.service.about.PartnerCaseService;
import com.sm.shoplook.util.FileUtil;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.base.BaseSearchVo;

/**
 * @author chenpy
 * @desc 合作伙伴和成功案例
 * @date 2018/8/16 16:31
 * @see
 */
@Controller
@RequestMapping("/partnerCase")
public class PartnerCaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(PartnerCaseController.class);
	@Autowired
	private PartnerCaseService partnerCaseService;
	@Autowired
	private FileUtil fileUtil;

	@RequestMapping(value = "/manager", method = RequestMethod.GET)
	public String partnerCaseManager() {
		return "/about/partner_case_manager";
	}

	/**
	 * @param baseSearchVo
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 合作伙伴和成功案例
	 */
	@RequestMapping(value = "/selectPartnerCasePage", method = RequestMethod.POST)
	@ResponseBody
	public ApiResult<Map<String, Object>> selectPartnerCasePage(BaseSearchVo baseSearchVo) {
		Map<String, Object> map = new HashMap<>();
		ApiResult<PageInfo<PartnerCase>> pageInfoApiResult = partnerCaseService.selectPartnerCasePage(baseSearchVo);
		PageInfo<PartnerCase> pageInfo = pageInfoApiResult.getData();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1, map);
	}

	/**
	 * @param id
	 * @param model
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 跳转合作伙伴和成功案例保存页面
	 */
	@RequestMapping(value = "/save_partner_case_page/{id}", method = RequestMethod.GET)
	public String addOrUpdateRolePage(@PathVariable(value = "id") Integer id, Model model) {
		LOGGER.info("id: {}", id);
		ApiResult<PartnerCase> partnerCaseApiResult = partnerCaseService.selectPartnerCaseById(id);
		PartnerCase partnerCase = partnerCaseApiResult.getData();
		if (partnerCase == null) {
			partnerCase = new PartnerCase();
			partnerCase.setType(2);
		}
		model.addAttribute("partnerCase", partnerCase);
		return "/about/add_update_partner_case";
	}

	/**
	 * @param ids
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 删除合作伙伴和成功案例
	 */
	@ResponseBody
	@RequestMapping(value = "/deletePartnerCase", method = RequestMethod.POST)
	public ApiResult deletePartnerCase(String ids) {
		String[] idList = ids.split(",");
		// 批量删除
		LOGGER.info("ids", ids);
		try {
			return partnerCaseService.deletePartnerCases(idList);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	/**
	 * @param partnerCase
	 * @param imgUrlFile
	 * @return
	 * @Author chenpy
	 * @Description
	 * @Date 保存
	 */
	@ResponseBody
	@RequestMapping(value = "/savePartnerCase", method = RequestMethod.POST)
	public ApiResult saveRole(PartnerCase partnerCase, MultipartFile imgUrlFile) {
		LOGGER.info("partnerCase", partnerCase);
		try {
			if (imgUrlFile != null) {
				String filePath = fileUtil.uploadFile(imgUrlFile);
				partnerCase.setImgUrl(filePath);
			}
			return partnerCaseService.savePartnerCase(partnerCase);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			return new ApiResult(ResultMessage.RESULT_ERROR_0);
		}
	}

	@RequestMapping(value = "/selectPartnerCaseByType", method = RequestMethod.GET)
	@ResponseBody
	public ApiResult<List<PartnerCase>> selectPartnerCaseByType(Integer type, Integer startNum, Integer count) {
		return partnerCaseService.selectPartnerCaseByType(type, startNum, count);
	}
}
